﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace ProjectRoughWork
{
    class ShotgunNerfAI : ProjectAIState
    {
        /// <summary>
        /// 3-Parameter constructor that initialize necessary variables
        /// </summary>
        /// <param name="initNerf">The Shotgun Nerf to be manipulated</param>
        /// <param name="target">The intended target's position</param>
        /// <param name="screen">The gameplay screen to delegate collision checks to</param>

        public ShotgunNerfAI(IActor initNerf, Vector3 target, GameplayScreen screen) : base(screen)
        {
            nerf = (ShotgunNerf)initNerf;
            mTarget = target;
        }

        public override void VOnUpdate()
        {
            IActor actorHit = CheckForCollision(nerf.getID(), nerf.ShooterID);
            if ((actorHit != null) &&
                (actorHit.getType().Equals("SquadActor"))
                )
            {
                //Deal damage to hit actor if enemy squad

                if (!actorHit.VIsGeometrical())
                {

                    EventDealDamage damageEvt = new EventDealDamage(actorHit.getID(), nerf.Damage, 0);
                    EventManager.GetInstance().VQueueEvent(damageEvt);

                    //Give XP to the shooter

                    EventGiveXP xpEvt = new EventGiveXP(nerf.ShooterID, 20);
                    EventManager.GetInstance().VTriggerEvent(xpEvt);

                    /*
                    SquadActor actorToDamage = (SquadActor)actorHit;
                    actorToDamage.GotHit(nerf.Damage);
                    */
                }

                //Remove this actor
                EventRemoveActor rmEvt = new EventRemoveActor(nerf.getID());
                EventManager.GetInstance().VQueueEvent(rmEvt);
                setDead(true);
            }
            else if ((nerf.Position.X < mTarget.X + 5) && (nerf.Position.X > mTarget.X - 5) &&
                        (nerf.Position.Z < mTarget.Z + 5) && (nerf.Position.Z > mTarget.Z - 5)
                )
            {
                //If the nerf has reached it's target but hasn't hit anything
                //      Remove this actor
                EventRemoveActor rmEvt = new EventRemoveActor(nerf.getID());
                EventManager.GetInstance().VQueueEvent(rmEvt);
                setDead(true);
            }
            else
            {
                //Seek Target using Steering

                Vector3 newVel = mTarget - nerf.getPosition();
                newVel.Normalize();
                newVel *= nerf.MaxAcceleration;
                nerf.setVelocity(/*nerf.getVelocity() +*/ newVel);

                EventMoveActor mvEvt = new EventMoveActor(nerf.getID(), nerf.Position + nerf.getVelocity());
                EventManager.GetInstance().VQueueEvent(mvEvt);

                //nerf.Position = nerf.Position + nerf.getVelocity();

            }
        }

        //Data Members

        ShotgunNerf nerf;
        float distanceTraveled;
    }
}
